﻿Public Class lobby
    Inherits System.Web.UI.Page

    Dim objDB As New DBConnect
    Dim sql As String
    Dim ds As New DataSet
    Dim userID As New user
    Dim gameID As Integer
    Dim gameName As String
    Dim matchNum As Integer
    Dim totalMatches As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        objDB = Session("db")
        userID = Session("user")

        lblpseudonym.Text = userID.pseudonym

        If Not IsPostBack Then
            sql = "SELECT * FROM Game"
            ds = objDB.getDataSet(sql)
            ddlGameType.DataSource = ds
            ddlGameType.DataTextField = "GameName"
            ddlGameType.DataValueField = "GameID"
            ddlGameType.DataBind()

            gvMatches.Visible = False

        End If

    End Sub

    Protected Sub ddlGameType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlGameType.SelectedIndexChanged
        gvMatches.Visible = True

        btnJoin.Visible = True

        gameID = ddlGameType.SelectedValue
        gameName = ddlGameType.SelectedItem.Text
        sql = "SELECT * FROM Match WHERE gameID=" & gameID
        ds = objDB.getDataSet(sql)
        totalMatches = ds.Tables(0).Rows.Count
        
        Dim counter2 As Integer
        Dim intCount As Integer
        gvMatches.DataSource = ds
        gvMatches.DataBind()
        Dim searchDS As New DataSet
        For counter As Integer = 0 To totalMatches - 1
            Dim matchID As String = gvMatches.Rows(counter).Cells(0).Text
            Dim newString As String = "SELECT * FROM Match_Player WHERE MatchID='" + matchID + "'"
            searchDS = objDB.getDataSet(newString)
            counter2 = searchDS.Tables.Count()
            intCount = searchDS.Tables(0).Rows.Count
            gvMatches.Rows(counter).Cells(1).Text = intCount
        Next



    End Sub

    Protected Sub btnJoin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnJoin.Click

            'Dim Cbox As CheckBox
            Dim matchNum As String = ""
        Dim oneSelect As Boolean = False


            'Scans each row of the gridview for selected checkboxes.  If one is checked, it sets matchNum to the appropriate match number
            'If two are checked, it will accept only the earlier match 

            For counter As Integer = 0 To (gvMatches.Rows.Count - 1)
                Dim chk As CheckBox = CType(gvMatches.Rows(counter).Cells(2).FindControl("chkSelectGame"), CheckBox)
                If chk.Checked Then
                    matchNum = gvMatches.Rows(counter).Cells(0).Text
                    Exit For
                End If
            Next

            If matchNum = "" Then
                Throw New SystemException("Select a Match")
            End If

            Session("matchID") = matchNum

        Dim sqlString As String = "INSERT INTO Match_Player(MatchID, ParticipantID) VALUES (" & matchNum & ", " + userID.participantID & ")"

            objDB.doUpdate(sqlString)


            gameID = ddlGameType.SelectedValue
            sql = "SELECT * FROM Match WHERE gameID=" & gameID
            ds = objDB.getDataSet(sql)
            totalMatches = ds.Tables(0).Rows.Count
        'MsgBox(totalMatches)

            gvMatches.DataSource = ds
            gvMatches.DataBind()

            Session("gameID") = gameID

        Response.Redirect("../ajaxGameEngine.aspx")
    End Sub

    Protected Sub gvMatches_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gvMatches.SelectedIndexChanged

    End Sub

    Protected Sub btnBackToMenu_Click(sender As Object, e As EventArgs) Handles btnBackToMenu.Click
        Response.Redirect("../MainMenu.aspx")
    End Sub
End Class